﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Cookie应用</title>
    <script>
        function setCookie(cname, cvalue, exdays) {
            let d = new Date();
            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
            let expires = "expires=" + d.toGMTString();
            document.cookie = cname + "=" + cvalue + "; " + expires;
        }
        function getCookie(cname) {
            let name = cname + "=";
            let ca = document.cookie.split(';');
            for (let i = 0; i < ca.length; i++) {
                let c = ca[i].trim();
                if (c.indexOf(name) === 0) return c.substring(name.length, c.length);
            }
            return "";
        }
        function checkCookie() {
            let user = getCookie("username");
            if (user !== "") {
                alert("再次欢迎您： " + user);
            }
            else {
                user = prompt("请输入您的姓名:", "");
                if (user !== "" && user !== null) {
                    setCookie("username", user, 30);
                }
            }
        }
    </script>
</head>
<body onload="checkCookie()">

</body>
</html>